package finassist.view;

import java.util.ArrayList;
import java.util.Currency;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import org.apache.struts2.interceptor.SessionAware;
import finassist.controller.LoginUserManager;
import finassist.model.Entiti;
import finassist.model.User;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * AJ Projects
 * UserLoginAction.java
 * Purpose: Action class for User Login Module. This class takes care of all login related actions.
 *
 * @author Karthick Natarajan
 * @version 1.0 9/10/13
 */

public class UserLoginAction extends ActionSupport implements SessionAware {

	private static final long serialVersionUID = 9149826260758390091L;
	private User user;
	private Long id;
	private Map session;
	private LoginUserManager linkController;
	private String role;
	private List<Entiti> entitiList = new ArrayList<Entiti>();
	private List<String> entitiListNames = new ArrayList<String>();
	private List<String> entitiTypes = new ArrayList<String>();
	private List<String> countries = new ArrayList<String>();
	
	private List<String> symbols = new ArrayList<String>();
	private List<String> optionList = new ArrayList<String>();

	private String entitiName;
		

	public UserLoginAction() {
		linkController = new LoginUserManager();
	}

	public String execute() {
		
		
			return SUCCESS;
	}
	
	//simple validation
		public void validate(){
			if("".equals(user.getUsername())){
				addFieldError("username", getText("username.required"));
			}
			if("".equals(user.getPassword())){
				addFieldError("password", getText("password.required"));
			}
					
		}

	public String login() {
		
		user = getUser();
		System.out.println(getUser());
		int result = 0;
              
         String[] locales = Locale.getISOCountries(); 
         for (String countryCode : locales) {
        	Locale obj = new Locale("", countryCode);
     		countries.add(obj.getDisplayCountry());
         }
         
         symbols.add("sds");
         symbols.add("Rs");
         
         optionList.add("A");
         optionList.add("NA");
         
		try {
		result =	linkController.check(user);
	 User user1 = (User) ActionContext.getContext().getSession().get("sesUser");
	 role = user1.getRole();
	 String fullName = user1.getFullName();
	 ActionContext.getContext().getSession().put("fullName", fullName);
	 
	this.entitiList = linkController.list();
	this.entitiTypes = linkController.listEntitiTypes();
		//	session.put("personlist", personList);
	
		for (int i = 0; i < entitiList.size(); i++) {
			entitiListNames.add(entitiList.get(i).getEntityName().toString());
		}
	 
	 
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(result ==10)
		return "ituser";
		else if(result==11)
		return "superuser";
		else
		return ERROR;
		
	}
	
	public static List<Long> getKeysFromValue(Map<Long, String> hm, String value) {
		List<Long> list = new ArrayList<Long>();
		for (Long o : hm.keySet()) {
			if (hm.get(o).equals(value)) {
				list.add(o);
			}
		}
		return list;
	}

	public User getUser() {
		return user;
	}
	
	public void setUser(User user) {
		this.user = user;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setSession(Map session) {
		this.session = session;
	}

	public Map getSession() {
		return session;

	}
	public String getRole() {
		return role;
	}

	public void setRole(String role) {
		this.role = role;
	}
	
	public List<String> getEntitiListNames() {
		return entitiListNames;
	}

	public void setEntitiListNames(List<String> entitiListNames) {
		this.entitiListNames = entitiListNames;
	}
	
	public String getEntitiName() {
		return entitiName;
	}

	public void setEntitiName(String entitiName) {
		this.entitiName = entitiName;
	}
	
	public List<String> getEntitiTypes() {
		return entitiTypes;
	}

	public void setEntitiTypes(List<String> entitiTypes) {
		this.entitiTypes = entitiTypes;
	}
	
	public List<String> getCountries() {
		return countries;
	}

	public void setCountries(List<String> countries) {
		this.countries = countries;
	}

	public List<String> getSymbols() {
		return symbols;
	}

	public void setSymbols(List<String> symbols) {
		this.symbols = symbols;
	}
	
	public List<String> getOptionList() {
		return optionList;
	}

	public void setOptionList(List<String> optionList) {
		this.optionList = optionList;
	}
}
